package util

import java.text.SimpleDateFormat
import java.util.Calendar
import org.apache.commons.lang.time.DateUtils
import org.json4s._
import org.json4s.JsonDSL._
import org.json4s.jackson.JsonMethods._
import  org.json4s.JsonAST._
import com.typesafe.config._

class ConfigContext(config: Config) {


  config.checkValid(ConfigFactory.defaultReference(), "util")

  def this() {
    this(ConfigFactory.load("application.conf"))
  }

  def get(path: String) =config.getString(path)

}
object Util {

  def getConfig(path:String)=new ConfigContext().get(path)

  /**
   *
   * @param day  DayofWeek("2015-05-28")
   * @return   2015-05-25
   */

   def DayofWeek(day:String)={

     try {
       val df = new SimpleDateFormat("yyyy-MM-dd")
       val  date = df.parse(day.toString())
       val  instance = Calendar.getInstance()
       instance.setTime(date)
       instance.setFirstDayOfWeek(Calendar.MONDAY)
       val  i = instance.get(Calendar.DAY_OF_WEEK)
       if(1 == i) {
         instance.add(Calendar.DAY_OF_MONTH, -1)
       }
       val  weekday = instance.get(Calendar.DAY_OF_WEEK)
       instance.add(Calendar.DATE, instance.getFirstDayOfWeek()-weekday)
       val tt=df.format(instance.getTime)
       tt
     //  val tt=df.format(DateUtils.addDays(date, -i + 2))
      // tt
     } catch {
       case e:Exception => Option("")
       case _:Throwable => Option("")
     }

   }

  /**
   *
   * @param day   //  MonthofYear("2015-05-25")
   * @return 2015-05-01
   */

  def MonthofYear(day:String)={
    try {
      val df = new SimpleDateFormat("yyyy-MM-dd")
      val  date = df.parse(day.toString())
      val  instance = Calendar.getInstance()
      instance.setTime(date)
      val  i = instance.get(Calendar.DAY_OF_MONTH)
      val tt=df.format(DateUtils.addDays(date, -i + 1))
      tt
    } catch {
      case e:Exception => Option("")
      case _:Throwable => Option("")
    }

  }

  /**
   *
   * @param day   getWeek("2015-05-25")
   * @return  2015-22
   */

  def getWeek(day:String)={
    try {
      val df = new SimpleDateFormat("yyyy-MM-dd")
      val  date = df.parse(day.toString())
      val  instance = Calendar.getInstance()
      instance.setTime(date)
      instance.setFirstDayOfWeek(Calendar.MONDAY)
      instance.setMinimalDaysInFirstWeek(4)
      val  i = instance.get((Calendar.WEEK_OF_YEAR))
      val year = instance.getWeekYear
      if (i<10) year.toString+"-0"+i.toString else  year.toString+"-"+i.toString
    } catch {
      case e:Exception => Option("")
      case _:Throwable => Option("")
    }

  }


  /** *
    *
    * @param day  getMonth("2015-01-25")
    * @return  2015-01
    */
  def getMonth(day:String)={
    try {
      val df = new SimpleDateFormat("yyyy-MM-dd")
      val  date = df.parse(day.toString())
      val  instance = Calendar.getInstance()
      instance.setTime(date)
      val  i = instance.get(Calendar.MONTH)+1
      val year = instance.get(Calendar.YEAR)
     if(i<10) year.toString+"-0"+i.toString else  year.toString+"-"+i.toString

    } catch {
      case e:Exception => Option("")
      case _:Throwable => Option("")
    }

  }

  /**
   *
   * @param day   //addDay("2015-01-25",-1)
   * @param incr
   * @return  2015-01-24
   */

  def addDay(day:String,incr:Int)={
    try {
      try {
        val df = new SimpleDateFormat("yyyy-MM-dd")
        val date = df.parse(day.toString())
        val instance = Calendar.getInstance()
        instance.setTime(date)
        val tt = df.format(DateUtils.addDays(date, incr))
        tt
      } catch {
        case e: Exception => Option("")
        case _: Throwable => Option("")
      }
    }

  }

  /**
   *
   * @param loc  replaceNull("null")
   * @return  Unknown
   */
  def  replaceNull(loc:String)=loc  match{
    case "null"=>"Unknown"
    case ""=>"Unknown"
    case null =>"Unknown"
    case loc =>loc

  }

}
